package com.kimho.menu.service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kimho.menu.domain.Menu;
import com.kimho.menu.persistence.MenuMapper;

@Service
public class MenuService {
	@Autowired
	private MenuMapper menuMapper;
	/**
	 * 获取用户所对应的权限菜单
	 * @param userId
	 * @return
	 */
	@Transactional
	public List<Menu> getMenuListByUserId(String userId) {
		Map<String,Object> param = new HashMap<String,Object>();
		List<Menu> mainMenuList = menuMapper.getMainMenuList(userId);
		Iterator<Menu> it = mainMenuList.iterator();
		//装载主菜单下所有的子菜单
		while(it.hasNext()) {
			Menu menu = it.next();
			//false:表示为主菜单
			System.out.println(it.toString());
			menu.setLeaf(false);
			Long id = menu.getId();
			param.put("userId", userId);
			param.put("id", id);
			List<Menu> subMenuList = menuMapper.getSubMenuList(param);
			menu.setChildren(subMenuList);
		}
		return mainMenuList;
	}
	/**
	 * 获取所有的菜单
	 * @return
	 */
	@Transactional
	public List<Menu> getMenuList() {
		List<Menu> menuList = menuMapper.getMenuList();
		return menuList;
	}
	@Transactional
	public List<Menu> getCosttypeList(String parent_id) {
		List<Menu> menuList = menuMapper.getCosttypeList(parent_id);
		return menuList;
	}
	
}
